package com.jfinal.bbsapp.common.kit;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author:yxk
 * @description: 实验室管理平台：日期工具类
 * @date:2018/12/23
 */
public class DateUtil {
    private static final Logger log = Logger.getLogger(DateUtil.class);

    /**
     * 默认日期格式
     */
    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

    /**
     * 日期字符串转日期
     */
    public static Date str2Date(String str, String format) {
        if (StringUtils.isBlank(str)) {
            return null;
        }
        // 如果没有指定字符串转换的格式，则用默认格式进行转换
        if (StringUtils.isBlank(format)) {
            format = DEFAULT_FORMAT;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date date = sdf.parse(str);
            return date;
        } catch (ParseException e) {
            log.error("",e);
        }
        return null;
    }

    /**
     * 日期转字符串
     */
    public static String date2Str(Date date, String format) {
        if (null == date) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static Long todayRemainSeconds() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        Date endTime = calendar.getTime();
        Date currentTime = new Date();
        return (endTime.getTime() - currentTime.getTime()) / 1000;

    }
    
    /**
     * 时间转换成时间戳,参数和返回值都是字符串
     * @param  s
     * @return ts
     * @throws ParseException
    */
   public static long dateToStamp(String s) throws ParseException {
	   long ts = 0;
       //设置时间模版
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       Date date = simpleDateFormat.parse(s);
       ts = date.getTime();
       return ts;
   }

}
